"""
使用requests请求代理服务器
请求http和https网页均适用
"""

import requests


def get_proxies_poll(i):
    return [get_proxies() for _ in range(i)]

def get_proxies():
    api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=or7gy2qb2vom9diupu8r&signature=txoj22nu6yp3ihg6dhcwtvgqtzj6vnud&num=1&sep=1"

    # 获取API接口返回的代理IP
    proxy_ip = requests.get(api_url).text

    # 用户名密码认证(私密代理/独享代理)
    username = "d2773115006"
    password = "rhpcau89"
    proxies = {
        "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip},
        "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": proxy_ip}
    }

    # 白名单方式（需提前设置白名单）
    # proxies = {
    #     "http": "http://%(proxy)s/" % {"proxy": proxy_ip},
    #     "https": "http://%(proxy)s/" % {"proxy": proxy_ip}
    # }

    # 要访问的目标网页
    target_url = "https://dev.kdlapi.com/testproxy"

    # 使用代理IP发送请求
    response = requests.get(target_url, proxies=proxies)

    # 获取页面内容
    if response.status_code == 200:
        print(response.text)